package com.wave.common.model;

import com.wave.common.enums.ErrorCode;
import com.wave.common.exception.WaveException;
import lombok.Data;

import java.io.Serializable;


@Data
public class Result<T> implements Serializable {

    private int code;
    private String message;
    private T data;
    private boolean success = true;

    public Result(ErrorCode code) {
        this.code = code.getCode();
        this.message = code.getMessage();
        this.data = null;
    }

    public Result(ErrorCode code, String msg) {
        this.code = code.getCode();
        this.message = msg;
        this.data = null;
    }

    public Result(ErrorCode code, T data) {
        this.code = code.getCode();
        this.message = code.getMessage();
        this.data = data;
    }

    public Result(String msg) {
        this.code = ErrorCode.SUCCESS.getCode();
        this.message = msg;
        this.data = null;
    }

    public Result(WaveException exception) {
        this.code = exception.getCode();
        this.message = exception.getMsg();
        this.data = null;
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ErrorCode.SUCCESS, data);
    }

    public static <T> Result<T> success() {
        return new Result<>(ErrorCode.SUCCESS);
    }

    public static <T> Result<T> status(boolean success) {
        return success ? success() : fail();
    }

    public static <T> Result<T> fail() {
        Result<T> result = new Result<>(ErrorCode.FAIL);
        result.success = false;
        return result;
    }

    public static <T> Result<T> fail(String msg) {
        Result<T> result = new Result<>(ErrorCode.FAIL, msg);
        result.success = false;
        return result;
    }

    public static <T> Result<T> fail(ErrorCode errorCode) {
        Result<T> result = new Result<>(errorCode);
        result.success = false;
        return result;
    }

    public static <T> Result<T> fail(ErrorCode errorCode, String msg) {
        Result<T> result = new Result<>(errorCode, msg);
        result.success = false;
        return result;
    }

    public static <T> Result<T> fail(WaveException e) {
        Result<T> result = new Result<>(e);
        result.success = false;
        return result;
    }

}
